<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Author: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\plat\logic;

/**
 * 租户逻辑
 */
class TenDev extends PlatBase
{

    public function getTenTenantTypeList($where = [], $field = true, $order = 'sort asc', $paginate = DB_LIST_ROWS)
    {
        $this->modelTenTenantType->setIgnoreTenantCheck();
        $list = $this->modelTenTenantType->getList($where, $field, $order, $paginate);
        return $list;
    }

    // 获取当前账号下所有的租户
    //列表
    public function getTenUserTenantList()
    {
        $where['a.ten_user_id'] = TEN_USER_ID;
        $this->modelTenTenantUser->alias('a');
        $this->modelTenTenantUser->join = [
            [SYS_DB_PREFIX . 'ten_tenant b', 'a.ten_tenant_id=b.id'],
            [SYS_DB_PREFIX . 'ten_user c', 'b.init_admin_id=c.id'],
        ];
        $this->modelTenTenantUser->setIgnoreTenantCheck();
        $tenant_list = $this->modelTenTenantUser->getList($where, 'b.*,c.username', '', false);
        return $tenant_list;
    }

    /**
     * 点击租户，获得租户信息
     * @param $data ['ten_tenant_id','ten_user_id']
     * @return array
     * @author: 开发人生 goodkfrs@qq.com
     * @Time: 2025/3/8 9:50
     */
    public function tenTenantInitUserData($data = [])
    {
        if (empty($data['ten_tenant_id'])) {
            throw_response_error('企业租户ID不能为空');
        }
        $where['ten_user_id'] = empty($data['ten_user_id']) ? TEN_USER_ID : $data['ten_user_id'];
        $where['ten_tenant_id'] = $data['ten_tenant_id'];
        $tenantId = $this->modelTenTenantUser->getValue($where, 'ten_tenant_id');
        if (empty($tenantId)) {
            throw_response_error('当前用户未绑定企业ID');
        }
        $where2['id'] = ['=', $tenantId];
        $this->modelTenTenant->setIgnoreTenantCheck();
        $tenantInfo = $this->modelTenTenant->getInfo($where2, '*');
        if (!empty($tenantInfo)) {
            if ($tenantInfo['visible'] != '1') {
                throw_response_error('企业租户已禁用');
            }
            $nowDate = date('Y-m-d H:i:s');
            if ($tenantInfo['stop_date'] < $nowDate) {
                throw_response_error('当前企业已到期，请续费!');
            }

            $tenantInfo = $tenantInfo->toArray();
            $auth = ['ten_tenant_id' => $tenantInfo['id'], 'init_admin_id' => $tenantInfo['init_admin_id'], TIME_UT_NAME => TIME_NOW];
            session('ten_tenant_info', $tenantInfo);
            session('ten_tenant_auth', $auth);
            session('ten_tenant_auth_sign', data_auth_sign($auth));
        }
        return $tenantInfo ? [RESULT_SUCCESS, '验证成功', url('index/index')] : [RESULT_ERROR, $this->modelTenTenant->getError()];
    }

    //平台端创建租户
    public function tenTenantAdd($data = [])
    {
        $validate_result = $this->validateTenDev->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenDev->getError()];
        }
        $url = url('show');
        $data['init_admin_id'] = TEN_USER_ID;
        $data['start_date'] = format_time(null, 'Y-m-d H:i:s');
        $data['stop_date'] = date_calc(null, '+15');
        $data['tenant_max_user'] = '3';
        $tenantId = $this->modelTenTenant->setInfo($data);

        //添加租户用户关系
        $result = $this->modelTenTenantUser->setInfo(['ten_tenant_id' => $tenantId, 'ten_user_id' => TEN_USER_ID]);

        $result && ten_log('新增', '新增租户：name' . $data['company']);
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenTenant->getError()];
    }
}